/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.util.HashMap;

/**
 *
 * @author elach_000
 */
public class Memory {

    private HashMap<Integer, Location> wood;
    private HashMap<Integer, Location> water;
    private HashMap<Integer, Location> stone;
    private HashMap<Integer, Location> gold;
    private HashMap<Integer, Location> animals;
    private HashMap<Integer, Integer> animalsReverse;

    public Memory() {
        this.wood = new HashMap<Integer, Location>();
        this.water = new HashMap<Integer, Location>();
        this.stone = new HashMap<Integer, Location>();
        this.gold = new HashMap<Integer, Location>();
        this.animals = new HashMap<Integer, Location>();
        this.animalsReverse = new HashMap<Integer, Integer>();
    }

    public boolean containsWood(Location location) {
        return wood.containsKey(location.hashCode());
    }

    public boolean containsGold(Location location) {
        return gold.containsKey(location.hashCode());
    }

    public boolean containsStone(Location location) {
        return stone.containsKey(location.hashCode());
    }

    public boolean containsWater(Location location) {
        return water.containsKey(location.hashCode());
    }

    public boolean containsAnimals(Location location) {
        for (Location l : this.animals.values()) {
            if (l.compare(location)) {
                return true;
            }
        }
        return false;
    }

    //Contains water,stone,gold,wood
    //Not for animals !!
    public boolean contains(Location location) {
        boolean contains = false;

        contains = containsWood(location) || containsStone(location) || containsGold(location) || containsWater(location);

        return contains;
    }

    void addWood(Location location) {
        this.wood.put(location.hashCode(), location);
    }

    void addGold(Location location) {
        this.gold.put(location.hashCode(), location);
    }

    void addStone(Location location) {
        this.stone.put(location.hashCode(), location);
    }

    void addWater(Location location) {
        this.water.put(location.hashCode(), location);
    }

    void addAnimals(int id, Location location) {
        this.animals.put(id, location);
        this.animalsReverse.put(location.hashCode(), id);
    }

    //Version temporaire, donne toujours la 1ere POI
    //Objectif : donner la plus proche ou random
    Location getPOI(Activity activity) {
        Location poi = null;

        switch (activity) {
            case Hunter:
                if (!animals.isEmpty()) {
                    poi = (Location) animals.values().toArray()[0];
                }
                break;
            case Lumberjack:
                if (!wood.isEmpty()) {
                    poi = (Location) wood.values().toArray()[0];
                }
                break;
            case Goldminer:
                if (!gold.isEmpty()) {
                    poi = (Location) gold.values().toArray()[0];
                }
                break;
            case Stoneminer:
                if (!stone.isEmpty()) {
                    poi = (Location) stone.values().toArray()[0];
                }
                break;
            case Builder:
                break;
        }
        return poi;
    }

    public void display() {
        System.out.println("Number of Wood Squares : " + wood.size());
        System.out.println("Number of Gold Squares : " + gold.size());
        System.out.println("Number of Stone Squares : " + stone.size());
        System.out.println("Number of Water Squares : " + water.size());
        System.out.println("Number of Animals Squares : " + animals.size());
    }

    void remove(Activity activity, Location target) {
        switch (activity) {
            case Hunter:
                removeAnimal(target);
                break;
            case Lumberjack:
                wood.remove(target.hashCode());
                break;
            case Goldminer:
                gold.remove(target.hashCode());
                break;
            case Stoneminer:
                stone.remove(target.hashCode());
                break;
            case Builder:
                break;
        }
    }

    public HashMap<Integer, Location> getWood() {
        return wood;
    }

    public HashMap<Integer, Location> getWater() {
        return water;
    }

    public HashMap<Integer, Location> getStone() {
        return stone;
    }

    public HashMap<Integer, Location> getGold() {
        return gold;
    }

    public HashMap<Integer, Location> getAnimals() {
        return animals;
    }

    private void removeAnimal(Location target) {
        if (target == null && !this.animals.containsValue(target)) {
            return;
        }

        int targetId = this.animalsReverse.get(target.hashCode());

        this.animals.remove(targetId);
        this.animalsReverse.remove(target.hashCode());
    }

    void removeAnimalWithID(Activity activity, int id) {
        this.animalsReverse.remove(this.animals.get(id).hashCode());    
        this.animals.remove(id);
    }
}
